package com.zork.data.generator.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zork.data.generator.mapper.TbNodeInfoMapper;
import com.zork.data.generator.mapper.TbNodeRelationInfoMapper;
import com.zork.data.generator.mapper.TopologyInfoMapper;
import com.zork.data.generator.model.NodeRelationInfo;
import com.zork.data.generator.model.TbNodeInfo;
import com.zork.data.generator.model.TopologyInfo;
import com.zork.data.generator.service.ArchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiesen
 */
@Service
@Slf4j
public class ArchServiceImpl implements ArchService {
    @Resource
    private TopologyInfoMapper topologyInfoMapper;

    @Resource
    private TbNodeInfoMapper tbNodeInfoMapper;

    @Resource
    private TbNodeRelationInfoMapper tbNodeRelationInfoMapper;

    @Override
    public List<NodeRelationInfo> nodeRelation() {
        List<NodeRelationInfo> list = new ArrayList<>();
        final LambdaQueryWrapper<TopologyInfo> topologyInfoLambdaQueryWrapper = Wrappers.lambdaQuery(TopologyInfo.class);
        final List<TopologyInfo> topologyInfos = topologyInfoMapper.selectList(topologyInfoLambdaQueryWrapper);
        if (null != topologyInfos && topologyInfos.size() > 0) {
            for (TopologyInfo topologyInfo : topologyInfos) {
                final String systemName = topologyInfo.getSystemname();
                final String topoinfo = topologyInfo.getTopoinfo();
                final JSONObject topoInfoJsonObject = JSON.parseObject(topoinfo);
                final JSONArray edges = topoInfoJsonObject.getJSONArray("edges");
                for (Object edge : edges) {
                    try {
                        final JSONObject jsonObject = JSON.parseObject(edge.toString());
                        final String source = jsonObject.getString("source");
                        final String target = jsonObject.getString("target");
                        LambdaQueryWrapper<TbNodeInfo> querySystemWrapperWithSource = Wrappers.lambdaQuery();
                        querySystemWrapperWithSource.eq(TbNodeInfo::getNodeid, source);
                        final TbNodeInfo sourceTbNodeInfo = tbNodeInfoMapper.selectOne(querySystemWrapperWithSource);
                        if (null == sourceTbNodeInfo) {
                            log.warn("根据源节点id [{}]未查询到节点信息.", source);
                        }
                        LambdaQueryWrapper<TbNodeInfo> querySystemWrapperWithTarget = Wrappers.lambdaQuery();
                        querySystemWrapperWithTarget.eq(TbNodeInfo::getNodeid, target);
                        final TbNodeInfo targetTbNodeInfo = tbNodeInfoMapper.selectOne(querySystemWrapperWithTarget);
                        if (null == targetTbNodeInfo) {
                            log.warn("根据目标节点id [{}]未查询到节点信息.", target);
                        }
                        if (null != sourceTbNodeInfo && null != targetTbNodeInfo) {
                            final NodeRelationInfo nodeRelationInfo = NodeRelationInfo.builder()
                                    .SysName(systemName).srcIp(sourceTbNodeInfo.getIp()).srcSysName(sourceTbNodeInfo.getSystemname())
                                    .destIp(targetTbNodeInfo.getIp()).destSysName(targetTbNodeInfo.getSystemname()).build();
                            list.add(nodeRelationInfo);

                            /// 将数据插入到数据库中
                            LambdaQueryWrapper<NodeRelationInfo> updateWrapper = Wrappers.lambdaQuery();
                            updateWrapper.eq(NodeRelationInfo::getSysName, nodeRelationInfo.getSysName())
                                    .eq(NodeRelationInfo::getSrcIp, nodeRelationInfo.getSrcIp())
                                    .eq(NodeRelationInfo::getDestIp, nodeRelationInfo.getDestIp());
                            final List<NodeRelationInfo> infos = tbNodeRelationInfoMapper.selectList(updateWrapper);
                            if (infos.size() > 0) {
                                tbNodeRelationInfoMapper.update(nodeRelationInfo, updateWrapper);
                            } else {
                                tbNodeRelationInfoMapper.insert(nodeRelationInfo);
                            }
                        }
                    } catch (Exception e) {
                        log.error("查询节点信息异常: {}", e.getMessage());
                    }

                }
            }
        }
        return list;
    }
}
